// Lou Decroo
// Modified 12-07-2009

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class GUIMaster extends JFrame{

	//page indices
	private static final int LOGIN = 1;
	private static final int COURSE_SEL = 2;
	private static final int CHANGE_PASS = 3;
	private static final int ADMIN = 4;
	private static final int COURSE_SURVEY = 5;
	private static final int PREVIEW_SURVEY = 6;
	private static final int MC_QUESTION = 7;
	private static final int OE_QUESTION = 8;
	private static final int VIEW_RESULTS = 9;
	//  system controller
	private static SystemController workHorse;

	//main display objects
	private JPanel topBar;
	private JLabel bannerHolder;
	private ImageIcon banner;
	private JPanel content;
	private JPanel navigationBar;
	private JButton back;
	private JButton forward;
	private JButton home, logout;
	private JScrollPane scroll;

	//Insert this in the GUIMaster class
	private static GUIMaster application;
	private Boolean loggedOut;






	//I tested just one panel to start
	//You will probably need an instance of each separate GUI in this class
	public LoginGUI loginGUI;
	public CourseSelectionGUI courseSelectGUI;
	public CourseSurveyGUI courseSurveyGUI;
	public ChangePasswordGUI changePassGUI;
	public Admin_page adminGUI;
	public AddMCQuestionGUI mcQuestionGUI;
	public AddOEQuestionGUI oeQuestionGUI;
	public ViewResultsGUI resultsGUI;


	// other globals
	private int pageIndex = 1;

	public GUIMaster(){

		super("GUIMaster");

		//      allocate system controller object
		workHorse = new SystemController();

		/*needs 3 main panels (maybe)
         a top one to display a banner
         middle to display content
         bottom one to display navigational buttons*/

		//add image to top panel
		banner = new ImageIcon("banner.jpg");
		bannerHolder = new JLabel(banner);
		
		topBar = new JPanel();
		topBar.setLayout(new GridLayout(1,1));
		topBar.setSize(640, 250);
		bannerHolder.setSize(640, 250);
		topBar.add(bannerHolder);
		topBar.setBackground(new Color(0,0,100));
		
		content = new JPanel();
		//content.setBackground(Color.red);
		scroll = new JScrollPane(content);
		scroll.getVerticalScrollBar();
		scroll.getHorizontalScrollBar();

		//              navigation bar never changes appearance but 
		// different functions can disable the buttons
		navigationBar = new JPanel(new FlowLayout());
		navigationBar.setBackground(Color.green);

		back = new JButton("back");
		forward = new JButton("forward");
		home = new JButton("home");
		logout = new JButton("logout");

		logout.addActionListener(new NavigationBarHandler());
		home.addActionListener(new NavigationBarHandler());
		back.addActionListener(new NavigationBarHandler());
		forward.addActionListener(new NavigationBarHandler());

		//navigationBar.add(back);
		navigationBar.add(home);
		//navigationBar.add(forward);
		navigationBar.add(logout);



		//add main panels
		add(topBar, BorderLayout.NORTH);
		add(scroll, BorderLayout.CENTER);
		add(navigationBar, BorderLayout.SOUTH);

		loginGUI = new LoginGUI();
		adminGUI = new Admin_page();
		changePassGUI = new ChangePasswordGUI();
		courseSelectGUI = new CourseSelectionGUI();
		courseSurveyGUI = new CourseSurveyGUI();
		mcQuestionGUI = new AddMCQuestionGUI();
		oeQuestionGUI = new AddOEQuestionGUI();
		resultsGUI = new ViewResultsGUI();
		
		// keep track of login state
		loggedOut = true;
		
		setSize(1000, 1000);
		setVisible(true);
	}


	public static GUIMaster getInstance()
	{
		System.out.println("Instance works");
		return application;
	}
	
	public static SystemController getWorkhorse()
	{
		System.out.println("getting workhorse works");
		return workHorse;
	}

	// method to handle a successful login
	public void successfulLogin(){
		loggedOut = false;
		pageIndex = COURSE_SEL;
		loginGUI.setEnabled(false);
		loginGUI.setVisible(false);
		loadPage();
	}
	
	// method to handle a successful admin login
	public void adminLogin(){
		loggedOut = false;
		pageIndex = ADMIN;
		loginGUI.setEnabled(false);
		loginGUI.setVisible(false);
		loadPage();
	}

	// method to handle user requests to change password
	public void changePassRequest(){
		pageIndex = CHANGE_PASS;
		changePassGUI = new ChangePasswordGUI();
		courseSelectGUI.setEnabled(false);
		courseSelectGUI.setVisible(false);
		loadPage();
	}

	// method to handle successful password change
	public void passChangeSuccessful(){
		pageIndex = COURSE_SEL;
		changePassGUI.setEnabled(false);
		changePassGUI.setVisible(false);
		loadPage();
	}

	// method to handle password request cancel
	public void passChangeCancelled(){
		pageIndex = COURSE_SEL;
		changePassGUI.setEnabled(false);
		changePassGUI.setVisible(false);
		loadPage();
	}

	// method to handle course selection
	public void courseSelected(){
		pageIndex = COURSE_SURVEY;
		courseSelectGUI.setEnabled(false);
		courseSelectGUI.setVisible(false);
		loadPage();
	}

	// method to handle request to add MC question
	public void mcQuestionAddRequested(){
		pageIndex = MC_QUESTION;
		courseSurveyGUI.setEnabled(false);
		courseSurveyGUI.setVisible(false);
		loadPage();
	}

	// method to handle request to add OE question
	public void oeQuestionAddRequested(){
		pageIndex = OE_QUESTION;
		courseSurveyGUI.setEnabled(false);
		courseSurveyGUI.setVisible(false);
		loadPage();
	}

	// method to handle add question request canceled
	public void questionAddCancelled(){
		pageIndex = COURSE_SURVEY;
		mcQuestionGUI.setEnabled(false);
		mcQuestionGUI.setVisible(false);
		oeQuestionGUI.setEnabled(false);
		oeQuestionGUI.setVisible(false);
		loadPage();
	}

	// method to handle successful question add
	public void questionAddSuccessful(){
		pageIndex = COURSE_SURVEY;
		mcQuestionGUI.setEnabled(false);
		mcQuestionGUI.setVisible(false);
		oeQuestionGUI.setEnabled(false);
		oeQuestionGUI.setVisible(false);
		loadPage();
	}

	// method to handle survey submission
	public void surveySubmitted(){
		pageIndex = COURSE_SEL;
		courseSurveyGUI.setEnabled(false);
		courseSurveyGUI.setVisible(false);
		loadPage();
	}

	// method to handle instructor course selection after surveys are no longer editable
	public void viewResults(){
		pageIndex = VIEW_RESULTS;
		courseSelectGUI.setEnabled(false);
		courseSelectGUI.setVisible(false);
		loadPage();
	}


	/********************NEED TO ADD METHODS TO HANDLE SURVEY RESULTS**************************
	 */




	/**
	 * Method used to load each page
	 */
	public void loadPage()
	{
		//            System.out.println(workHorse.pingDB());
		System.out.println("Run");
		switch (pageIndex)
		{
		case LOGIN://call special login function
			System.out.println("Login case");
			loadLoginGUI(); break;
		case COURSE_SEL:
			System.out.println("Course Selection case");
			loadCourseSelectionGUI(); break;
		case CHANGE_PASS:
			System.out.println("Change password case");
			loadChangePassGUI(); break;
		case ADMIN:
			System.out.println("Admin case");
			loadAdminGUI(); break;
		case COURSE_SURVEY:
			System.out.println("Course survey case");
			loadCourseSurveyGUI(); break;
		case MC_QUESTION:
			System.out.println("Add multiple choice question case");
			loadMCQuestionGUI(); break;
		case OE_QUESTION:
			System.out.println("Add open ended question case");
			loadOEQuestionGUI(); break;
		case VIEW_RESULTS:
			System.out.println("View results case");
			loadResultsGUI(); break;
		default: System.out.println("Error in switch statement");
		}

	}

	// method to clear all panels from the main panel
	public void clearPages()
	{
		loginGUI.setEnabled(false);
		loginGUI.setVisible(false);

		adminGUI.setEnabled(false);
		adminGUI.setVisible(false);
		
		courseSelectGUI.setEnabled(false);
		courseSelectGUI.setVisible(false);

		changePassGUI.setEnabled(false);
		changePassGUI.setVisible(false);

		courseSurveyGUI.setEnabled(false);
		courseSurveyGUI.setVisible(false);

		mcQuestionGUI.setEnabled(false);
		mcQuestionGUI.setVisible(false);

		oeQuestionGUI.setEnabled(false);
		oeQuestionGUI.setVisible(false);
		
		resultsGUI.setEnabled(false);
		resultsGUI.setVisible(false);
		
		loginGUI = null;
		adminGUI = null;
		changePassGUI = null;
		courseSelectGUI = null;
		courseSurveyGUI = null;
		mcQuestionGUI = null;
		oeQuestionGUI = null;
		resultsGUI = null;
		
		loginGUI = new LoginGUI();
		adminGUI = new Admin_page();
		changePassGUI = new ChangePasswordGUI();
		courseSelectGUI = new CourseSelectionGUI();
		courseSurveyGUI = new CourseSurveyGUI();
		mcQuestionGUI = new AddMCQuestionGUI();
		oeQuestionGUI = new AddOEQuestionGUI();
		resultsGUI = new ViewResultsGUI();
	}


	// method to load login panel
	public void loadLoginGUI()
	{
		//loginGUI = new LoginGUI();
		loginGUI.setEnabled(true);
		loginGUI.setVisible(true);
		content.add(loginGUI, BorderLayout.CENTER);
		System.out.println("end load page function");
	}

	// method to load course selection page
	public void loadCourseSelectionGUI()
	{
		//courseSelectGUI = new CourseSelectionGUI();
		courseSelectGUI.setEnabled(true);
		courseSelectGUI.setVisible(true);
		content.add(courseSelectGUI, BorderLayout.CENTER);
		System.out.println("Success!");
	}

	// method to load password page
	public void loadChangePassGUI()
	{
		//changePassGUI = new ChangePasswordGUI();
		changePassGUI.setEnabled(true);
		changePassGUI.setVisible(true);
		content.add(changePassGUI, BorderLayout.CENTER);

	}

	// method to load admin page
	public void loadAdminGUI()
	{
		//adminGUI = new Admin_page();
		adminGUI.setEnabled(true);
		adminGUI.setVisible(true);
		content.add(adminGUI, BorderLayout.CENTER);
	}

	// method to load survey
	public void loadCourseSurveyGUI()
	{
		//courseSurveyGUI = new CourseSurveyGUI(); // may add this later
		courseSurveyGUI.setEnabled(true);
		courseSurveyGUI.setVisible(true);
		content.add(courseSurveyGUI, BorderLayout.CENTER);

	}

	// method to load add multiple choice gui
	public void loadMCQuestionGUI(){
		//mcQuestionGUI = new AddMCQuestionGUI();
		mcQuestionGUI.setEnabled(true);
		mcQuestionGUI.setVisible(true);
		content.add(mcQuestionGUI, BorderLayout.CENTER);
	}

	// method to load add open ended gui
	public void loadOEQuestionGUI(){
		//oeQuestionGUI = new AddOEQuestionGUI();
		oeQuestionGUI.setEnabled(true);
		oeQuestionGUI.setVisible(true);
		content.add(oeQuestionGUI, BorderLayout.CENTER);	
	}

	public void loadResultsGUI(){
		// resultsGUI = new ViewResultsGUI();
		resultsGUI.setEnabled(true);
		resultsGUI.setVisible(true);
		content.add(resultsGUI, BorderLayout.CENTER);
	}


	// handle navigation buttons
	private class NavigationBarHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			if (event.getSource() == back)
			{
				// need to add code to keep track of order that pages were visited
				System.out.println("go back one page");
			}

			if (event.getSource() == forward)
			{
				// must have already went back to be active
				System.out.println("go forward one page");
			}

			if (event.getSource() == home)
			{
				if(!loggedOut){
					//courseSelectGUI = null;
					//courseSelectGUI = new CourseSelectionGUI();
					
					pageIndex = COURSE_SEL;
					//clearPages();
					loadPage();
				}
			}

			if (event.getSource() == logout)
			{
				loggedOut = true;
				//loginGUI = new LoginGUI();
				pageIndex = LOGIN;
				clearPages();
				workHorse.reset();
				loadPage();
			}

		}
	}

	//I moved the main function in here so the PaperlessSurveySystem class should
	//become obsolete.
	public static void main(String args[])
	{
		application = new GUIMaster();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		application.loadPage();
	}

}